/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package base;

import java.util.ArrayList;

/**
 *
 * @author Tue
 */
public class SynchronousQueue<E> {

    private ArrayList<E> queue;

    public SynchronousQueue(){
        queue = new ArrayList<E>();
    }
    public synchronized E dequeue(){
        try {
            while (!(queue.size() > 0)) {
                this.wait();
            }
            E e = queue.get(0);
            queue.remove(0);
            return e;
        } catch (InterruptedException ie) {
            ie.printStackTrace();
            return null;
        }
    }
    
    public synchronized void incoming(E e) {
        queue.add(e);
        this.notifyAll();
    }

    // Add a wave to the front of the wave queue.
    public synchronized void incomingFront(E e) {
        queue.add(0, e);
        this.notifyAll();
    }

    public synchronized boolean hasElements() {
        return queue.size() > 0;
    }
    public synchronized int size(){
        return queue.size();
    }

}
